package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.nextfm.dao.AlbumDao;
import br.nextfm.dao.ArtistaDao;
import br.nextfm.model.Musica;

public class RemoveAlbum extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		AlbumDao dao = new AlbumDao();
		ArtistaDao artistaDao = new ArtistaDao();
		
		if (dao.get(id).getMusicas() != null) {
			
			for (Musica musica : dao.get(id).getMusicas()) {
				musica.setAlbum(null);
			}
			
		}
		
		try { 
			
			dao.delete(id); 
		
			request.setAttribute("albums", dao.getAll());
			request.setAttribute("artistas", artistaDao.getAll());
			request.getRequestDispatcher("album.jsp").forward(request, response);
		}
		
		catch (Exception e) {
			
			request.setAttribute("albums", dao.getAll());
			request.setAttribute("artistas", artistaDao.getAll());
			request.getRequestDispatcher("album.jsp").forward(request, response);
			
		}
	}

}
